package com.changgou.service.goods.service.impl;

import com.changgou.goods.pojo.Brand;
import com.changgou.service.goods.dao.BrandMapper;
import com.changgou.service.goods.service.BrandService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.Map;

@Service
public class BrandServiceImpl implements BrandService{

    //品牌列表查询
    @Autowired
    private BrandMapper brandMapper;

    public List<Brand> findAllBrand() {
        List<Brand> brands = brandMapper.selectAll();
        return brands;
    }

    //根据品牌的id查询信息
    public Brand findById(Integer id) {
        Brand brand = brandMapper.selectByPrimaryKey(id);
        return brand;
    }

    //新增品牌信息
    @Transactional
    public void add(Brand brand) {
        brandMapper.insertSelective(brand);
    }

    //修改品牌信息
    @Transactional
    public void update(Brand brand) {
         brandMapper.updateByPrimaryKey(brand);
    }


    //根据id删除品牌信息
    @Transactional
    public void deleteById(Integer id) {
        brandMapper.deleteByPrimaryKey(id);
    }

    //条件查询 1.品牌名称 2.关键字(首字母)
    public List<Brand> findByCondition(Map<String, Object> searchMap) {
        Example example =new Example(Brand.class);
        Example.Criteria criteria = example.createCriteria();
        //根据品牌名称进行查询 模糊查询
        if(searchMap!=null){
            //获取品牌的名称
            Object name = searchMap.get("name");
            //获取关键字(首字母)
            Object letter = searchMap.get("letter");
            if(name!=null&&!"".equals(name)){
                criteria.andLike("name","%"+name+"%");
            }
            if(letter!=null&&!"".equals(letter)){
                criteria.andEqualTo("letter",letter);
            }
        }
       return brandMapper.selectByExample(example);
    }


    //分页查询
    public Page<Brand> findPage(Integer page, Integer size) {
        PageHelper.startPage(page,size);
        Page<Brand> list = (Page<Brand>) brandMapper.selectAll();
        return list;
    }


    //分页条件查询
    public Page<Brand> findPageByCondition(Map<String, Object> searchMap, Integer page, Integer size) {
        //设置分页
        PageHelper.startPage(page,size);
        //设置条件
        Example example =new Example(Brand.class);
        Example.Criteria criteria = example.createCriteria();
        if(searchMap!=null){
           //根据品牌名称进行查询 模糊查询
            Object name = searchMap.get("name");
            if(name!=null&&!"".equals(name)){
                criteria.andLike("name","%"+name+"%");
            }
            Object letter = searchMap.get("letter");
            if(letter!=null&&!"".equals(letter)){
                criteria.andEqualTo("letter",letter);
            }
        }
        Page<Brand> list = (Page<Brand>) brandMapper.selectByExample(example);
        return list;
    }
}
